package com.csot.ry.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import com.csot.model.TotalIp;
import com.csot.model.TotalSegment;
import com.csot.ry.util.CacheKitUtil;
import com.jfinal.plugin.activerecord.Db;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.csot.common.annotation.Log;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.AjaxResult;
import com.csot.common.enums.BusinessType;
import com.csot.ry.domain.TTotalIp;
import com.csot.ry.service.ITTotalIpService;
import com.csot.common.utils.poi.ExcelUtil;
import com.csot.common.core.page.TableDataInfo;

/**
 * 省级ip明细Controller
 * 
 * @author 刘沙
 * @date 2022-05-31
 */
@RestController
@RequestMapping("/ipmanagement/totalip")
public class TTotalIpController extends BaseController {
	@Autowired
	private ITTotalIpService tTotalIpService;

	/**
	 * 查询省级ip明细列表
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:list')")
	@GetMapping("/list")
	public TableDataInfo list(TTotalIp tTotalIp) {
		String parentId = getLoginUser().getUser().getDept().getParentId() + "";
		if (CacheKitUtil.ip_dept != null && StrUtil.isNotEmpty(parentId)) {
			tTotalIp.setIpCity(CacheKitUtil.ip_dept.get(parentId + ""));
		}
		startPage();
		List<TTotalIp> list = tTotalIpService.selectTTotalIpList(tTotalIp);
		return getDataTable(list);
	}

	/**
	 * 导出省级ip明细列表
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:export')")
	@Log(title = "省级ip明细", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, TTotalIp tTotalIp) {
		// 测试更新子表,TotalSegmentDetail好用了之后删除此方法
//        set1();
		String parentId = getLoginUser().getUser().getDept().getParentId() + "";
		if (CacheKitUtil.ip_dept != null && StrUtil.isNotEmpty(parentId)) {
			tTotalIp.setIpCity(CacheKitUtil.ip_dept.get(parentId + ""));
		}
		List<TTotalIp> list = tTotalIpService.selectTTotalIpList(tTotalIp);
		ExcelUtil<TTotalIp> util = new ExcelUtil<TTotalIp>(TTotalIp.class);
		util.exportExcel(response, list, "省级ip明细数据");
	}

	private boolean set1() {
		List<TotalSegment> csList = getSlot();
		if (CollUtil.isEmpty(csList)) {
			return true;
		}

		csList.forEach(f -> {
			Db.tx(() -> {
				List<TotalIp> ciList = new ArrayList<>();
				for (long i = f.getIpStartI(); i <= f.getIpEndI(); i++) {
					TotalIp ci = getTotalIpByAll(f);
//                        BeanUtil.copyProperties(f, ci, "id");
					ci.setIpAddressI(i);
					ci.setIpAddress(NetUtil.longToIpv4(i));
					ciList.add(ci);
				}
				List<List<TotalIp>> splitIps = ListUtil.split(ciList, 1000);
				for (List<TotalIp> ips : splitIps) {
					Db.batchSave(ips, ips.size());
				}
				return true;
			});
		});
		return true;
	}

	private TotalIp getTotalIpByAll(TotalSegment f) {
		TotalIp ci = new TotalIp();
		ci.setCreateBy(f.getCreateBy());
		ci.setCreateTime(f.getCreateTime());
		ci.setIpCity(f.getIpCity());
//        ci.setIpEquipment();
//        ci.setIpGateway();
		ci.setIpManagement(f.getIpManagement());
		ci.setIpSegmentId(f.getId());
		ci.setIpStatus(f.getIpStatus());
		ci.setIpType(f.getIpType());
		ci.setIpUser(f.getIpUser());
//        ci.setIpUseType();
//        ci.setRecordStatus();
//        ci.setRecordType();
		ci.setRemark(f.getRemark());
		ci.setUpdateBy(f.getUpdateBy());
		ci.setUpdateTime(f.getUpdateTime());
		return ci;
	}

	/**
	 * 获取省级ip明细详细信息
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(tTotalIpService.selectTTotalIpById(id));
	}

	/**
	 * 新增省级ip明细
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:add')")
	@Log(title = "省级ip明细", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody TTotalIp tTotalIp) {
		return toAjax(tTotalIpService.insertTTotalIp(tTotalIp));
	}

	private List<TotalSegment> getSlot() {
		// TODO Auto-generated method stub

		TotalSegment dao = new TotalSegment().dao();
		List<TotalSegment> csList = dao
				.find("select * from t_total_segment t where id not IN (select ip_segment_id from t_total_ip)");
		return csList;

	}

	/**
	 * 修改省级ip明细
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:edit')")
	@Log(title = "省级ip明细", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody TTotalIp tTotalIp) {
		return toAjax(tTotalIpService.updateTTotalIp(tTotalIp));
	}

	/**
	 * 删除省级ip明细
	 */
	@PreAuthorize("@ss.hasPermi('ipmanagement:totalip:remove')")
	@Log(title = "省级ip明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(tTotalIpService.deleteTTotalIpByIds(ids));
	}
}
